<template>
  <div id="workDate">
    <div class="selectYear">
      <el-select v-model="selectYear" size="mini" @change="updateCalendar">
        <el-option
          v-for="item in years"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select v-model="selectMonth" size="mini" @change="updateCalendar">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </div>
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{ date, data }">
        <p
          :class="data.isSelected ? 'is-selected' : ''"
          @click="onCalendar(date)"
        >
          {{ data.day.split("-")[2] }}
          <span v-if="isWeek(date)" class="text">休</span>
        </p>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: 'WorkDate',
  data() {
    return {
      years: [],
      selectYear: new Date().getFullYear(),
      selectMonth: new Date().getMonth() + 1,
      currentDate: new Date()
    }
  },

  created() {
    this.years = Array.from(
      Array(11),
      (val, index) => this.selectYear - 5 + index
    )
    // this.years = Array(11).fill('').map((val, index) => (this.selectYear - 5 + index))
  },
  // 定义方法
  methods: {
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    },
    updateCalendar() {
      const date = this.selectYear + '-' + this.selectMonth
      this.currentDate = new Date(date)
    },
    onCalendar(date) {
      this.selectYear = date.getFullYear()
      this.selectMonth = date.getMonth() + 1
    }
  }
}
</script>

<style scoped lang="scss">
.selectYear {
  display: flex;
  justify-content: flex-end;
  .el-select {
    width: 80px;
    margin: 0 3px;
  }
}

::v-deep .el-calendar-day {
  height: auto;
  text-align: center;
  line-height: 30px;
  padding: 0;
  p {
    padding: 20px 0;
    margin: 0;
  }
}
::v-deep .el-calendar-table__row td,
::v-deep .el-calendar-table tr td:first-child,
::v-deep .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}

::v-deep thead {
  th {
    font-size: 14px;
    font-weight: bold;
  }
}
::v-deep .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
::v-deep .el-calendar__header {
  display: none;
}

.el-calendar {
  .text {
    font-size: 12px;
    color: #fff;
    background: #fa7c4d;
    width: 20px;
    height: 20px;
    border-radius: 20px;
    display: inline-block;
    line-height: 20px;
    text-align: center;
  }
}
</style>
